<?php

namespace app\common\model\xilujob;

use app\common\library\Result;
use think\Db;
use think\Exception;
use think\Model;

class ResumeActionLog extends Model
{
    protected $name = 'xilujob_resume_action_log';
    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = false;

    /**
     * 记录操作 判断是否有可用次数并扣减
     * @param $companyId
     * @param $userId
     * @param $resumeId
     */
    public static function record($companyId, $userId, $resumeId) {
        $log = static::where([
            'company_id'=>$companyId, 'user_id'=>$userId, 'resume_id'=>$resumeId
        ])->count();
        if($log) {
            return true;
        }
        $enterpriseinfo = EnterpriseInfo::field('id,resume_action_num')->where('company_id', $companyId)->find();
        if(!$enterpriseinfo) {
            throw new Exception(__('no record'));
        }
        if(Resume::where('id',$resumeId)->count()==0) {
            throw new Exception(__('no record'));
        }
        $resume_action_num = Config::getdatas("resume_action_num") ?? 0;
        if($resume_action_num > 0) {
            // 有免费次数 , 判定是否当日还有剩余
            $todayNum = static::where('company_id',$companyId)
                ->count();
            $freeNum = $resume_action_num > $todayNum ? $resume_action_num - $todayNum : 0;
        }else {
            $freeNum = 0;
        }
        if($freeNum <= 0 && $enterpriseinfo->resume_action_num <= 0) {
            throw new Exception("您购买的套餐中查看简历数已用尽，请前往购买");
        }
        Db::startTrans();
        try {
            $log = new static();
            $log->save(['company_id' => $companyId, 'user_id' => $userId, 'resume_id' => $resumeId]);
            if($freeNum <= 0) {
                $enterpriseinfo->save(['resume_action_num'=>Db::raw('resume_action_num-1')]);
            }
            Db::commit();
        }catch (\Exception $e) {
            Db::rollback();
            throw $e;
        }
        return true;
    }

    /**
     * 获取企业简历查看数可用操作次数
     * @param $companyId
     * @param $type
     * @return int
     */
    public static function getAvailableNum($companyId,$type) {
        $enterpriseinfo = EnterpriseInfo::field('id,resume_action_num,interview_num')->where('company_id', $companyId)->find();
        if(!$enterpriseinfo) {
            return 0;
        }
        if($type == 'resume_view'){
            $resume_action_num = Config::getdatas("resume_action_num") ?? 0;
            if($resume_action_num > 0) {
                // 有免费次数 , 判定是否当日还有剩余
                $todayNum = static::where('company_id',$companyId)
                    //->whereTime('createtime','m')
                    ->count();
                $freeNum = $resume_action_num > $todayNum ? $resume_action_num - $todayNum : 0;
            }else {
                $freeNum = 0;
            }
            $return = intval($freeNum + $enterpriseinfo->resume_action_num);
        }else{
            $interview_num = Config::getdatas("interview_num") ?? 0;
            if($interview_num > 0) {
                // 有免费次数 , 判定是否当日还有剩余
                $todayNum = JobInterview::where('company_id',$companyId)
                    //->whereTime('createtime','m')
                    ->count();
                $freeNum = $interview_num > $todayNum ? $interview_num - $todayNum : 0;
            }else {
                $freeNum = 0;
            }
            $return = intval($freeNum + $enterpriseinfo->interview_num);
        }
        return $return;
    }

    public static function hasLog($companyId, $userId, $resumeId) {
        return static::where([
            'company_id'=>$companyId, 'user_id'=>$userId, 'resume_id'=>$resumeId
        ])->count() > 0;
    }

}
